/**
 * 纯蒙版控制器
 * 统一管理纯蒙版功能的各个组件
 */

import type { ActiveBarrage, DetectionArea } from '../../types'
import { PureMaskEngine, type PureMaskConfig } from './PureMaskEngine'
import { PureMaskVisualizer, type PureMaskVisualizerStyle } from './PureMaskVisualizer'

export interface PureMaskControllerOptions {
  /** 纯蒙版配置 */
  maskConfig?: Partial<PureMaskConfig>
  /** 可视化样式配置 */
  visualizerStyle?: Partial<PureMaskVisualizerStyle>
  /** 是否启用调试模式 */
  debugMode?: boolean
}

export class PureMaskController {
  private engine: PureMaskEngine
  private visualizer: PureMaskVisualizer | null = null
  private debugMode: boolean = false

  constructor(options: PureMaskControllerOptions = {}) {
    this.engine = new PureMaskEngine(options.maskConfig)
    this.debugMode = options.debugMode || false
  }

  /**
   * 初始化可视化器
   */
  initVisualizer(ctx: CanvasRenderingContext2D, style?: Partial<PureMaskVisualizerStyle>): void {
    this.visualizer = new PureMaskVisualizer(ctx, this.engine.getConfig(), style)
  }

  /**
   * 更新蒙版配置
   */
  updateMaskConfig(config: Partial<PureMaskConfig>): void {
    this.engine.updateConfig(config)
    if (this.visualizer) {
      this.visualizer.updateConfig(this.engine.getConfig())
    }
  }

  /**
   * 更新可视化样式
   */
  updateVisualizerStyle(style: Partial<PureMaskVisualizerStyle>): void {
    if (this.visualizer) {
      this.visualizer.updateStyle(style)
    }
  }

  /**
   * 设置调试模式
   */
  setDebugMode(enabled: boolean): void {
    this.debugMode = enabled
  }

  /**
   * 获取当前配置
   */
  getConfig(): PureMaskConfig {
    return this.engine.getConfig()
  }

  /**
   * 处理单个弹幕的纯蒙版效果
   */
  processBarrage(barrage: ActiveBarrage, detectionAreas: DetectionArea[]): number {
    return this.engine.processBarrageMask(barrage, detectionAreas)
  }

  /**
   * 批量处理弹幕的纯蒙版效果
   */
  processBarrages(barrages: ActiveBarrage[], detectionAreas: DetectionArea[]): void {
    this.engine.processBarragesMask(barrages, detectionAreas)
  }

  /**
   * 渲染调试信息
   */
  renderDebug(areas: DetectionArea[], canvasWidth: number, canvasHeight: number): void {
    if (!this.debugMode || !this.visualizer) return
    
    this.visualizer.renderDebugOverlay(areas, canvasWidth, canvasHeight)
  }

  /**
   * 检查弹幕是否在影响范围内
   */
  isBarrageInInfluenceRange(barrage: ActiveBarrage, areas: DetectionArea[]): boolean {
    return this.engine.isBarrageInInfluenceRange(barrage, areas)
  }

  /**
   * 启用纯蒙版效果
   */
  enable(): void {
    this.updateMaskConfig({ enabled: true })
  }

  /**
   * 禁用纯蒙版效果
   */
  disable(): void {
    this.updateMaskConfig({ enabled: false })
  }

  /**
   * 切换纯蒙版效果
   */
  toggle(): boolean {
    const currentConfig = this.getConfig()
    const newEnabled = !currentConfig.enabled
    this.updateMaskConfig({ enabled: newEnabled })
    return newEnabled
  }

  /**
   * 设置蒙版透明度
   */
  setMaskOpacity(opacity: number): void {
    // 确保透明度在有效范围内
    const clampedOpacity = Math.max(0, Math.min(1, opacity))
    this.updateMaskConfig({ maskOpacity: clampedOpacity })
  }

  /**
   * 设置平滑边缘
   */
  setSmoothEdges(enabled: boolean, blurRadius?: number): void {
    const config: Partial<PureMaskConfig> = { smoothEdges: enabled }
    if (blurRadius !== undefined) {
      config.blurRadius = Math.max(0, blurRadius)
    }
    this.updateMaskConfig(config)
  }

  /**
   * 获取统计信息
   */
  getStats(): {
    enabled: boolean
    maskOpacity: number
    smoothEdges: boolean
    blurRadius: number
    debugMode: boolean
  } {
    const engineStats = this.engine.getStats()
    return {
      ...engineStats,
      debugMode: this.debugMode
    }
  }

  /**
   * 重置为默认配置
   */
  reset(): void {
    this.updateMaskConfig({
      enabled: true,
      maskOpacity: 0.0,
      smoothEdges: true,
      blurRadius: 5
    })
  }

  /**
   * 销毁控制器
   */
  destroy(): void {
    this.visualizer = null
  }
}
